/*
 * YuebaoShoppingGoods.cpp
 *
 *  Created on: 2011-3-11
 *      Author: 
 */

#include "YuebaoShoppingGoods.h"
#include "fWml.h"
#include "ProjectsDefines.h"
#include "XmlNodeCreater.h"
#include "BetterUserHBufC.h"

YuebaoShoppingGoods* YuebaoShoppingGoods::NewLC( )
{
    YuebaoShoppingGoods* self = new ( ELeave ) YuebaoShoppingGoods( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

YuebaoShoppingGoods* YuebaoShoppingGoods::NewL( )
{
    YuebaoShoppingGoods* self = YuebaoShoppingGoods::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

YuebaoShoppingGoods::YuebaoShoppingGoods( )
{

}

void YuebaoShoppingGoods::ConstructL( )
{

}

YuebaoShoppingGoods::~YuebaoShoppingGoods( )
{
    SAFE_DELETE( _name );
    SAFE_DELETE( _comment );
    SAFE_DELETE( _time );
    SAFE_DELETE( _unit );
    SAFE_DELETE( _order_id );
}
_LIT( kGoodsIDNode, "id");
_LIT( kGoodsNameNode, "name");
_LIT( kGoodsPriceNode, "price");
_LIT( kGoodsNumberNode, "number");
_LIT( kGoodsPurchasedNode, "purchased");
_LIT( kGoodsCommentNode, "comment");
_LIT( kGoodsTimeNode, "time");
_LIT( kGoodsUnitNode, "unit");
_LIT( kGoodsOrderIDNode, "orderid");
YuebaoShoppingGoods* YuebaoShoppingGoodsParser::Parse( CElement* elementGoods )
{
    if( elementGoods == 0 || elementGoods->ElementCount( ) == 0 )
    {
        return NULL;
    }
    YuebaoShoppingGoods* goods = YuebaoShoppingGoods::NewLC( );

    goods->_id = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kGoodsIDNode ) );
    goods->_name = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsNameNode ) );
    goods->_price = BetterUtility::GetElementTextAsFloat( elementGoods->GetElement( kGoodsPriceNode ) );
    goods->_number = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kGoodsNumberNode ) );
    goods->_purchased = ( BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kGoodsPurchasedNode ) ) == 1 );
    goods->_comment = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsCommentNode ) );
    goods->_time = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsTimeNode ) );
    goods->_unit = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsUnitNode ) );
    goods->_order_id = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsOrderIDNode ) );
    CleanupStack::Pop( ); // goods;
    return goods;
}
_LIT( kGoodsNodeStart,"<goods>" );
_LIT( kGoodsNodeEnd ,"</goods>");

void YuebaoShoppingGoodsParser::BuildXmlGoodsNode( YuebaoShoppingGoods* goods, TDes& buffer )
{
    //buffer.SetLength( 0 );
    if( !goods )
    {
        return;
    }

    buffer.Append( kGoodsNodeStart );
    // <node>value</node>
    // <node/>

    // id
    XmlNodeCreater::BuildIntNode( kGoodsIDNode, goods->_id, buffer );
    // name
    XmlNodeCreater::BuildTextNode( kGoodsNameNode, goods->_name, buffer );
    // price
    XmlNodeCreater::BuildFloatNode( kGoodsPriceNode, goods->_price, buffer );
    // unit
    XmlNodeCreater::BuildTextNode( kGoodsUnitNode, goods->_unit, buffer );
    // number
    XmlNodeCreater::BuildIntNode( kGoodsNumberNode, goods->_number, buffer );
    //purchased
    XmlNodeCreater::BuildIntNode( kGoodsPurchasedNode, goods->_purchased, buffer );
    //comment
    XmlNodeCreater::BuildTextNode( kGoodsCommentNode, goods->_comment, buffer );
    //time
    XmlNodeCreater::BuildTextNode( kGoodsTimeNode, goods->_time, buffer );
    //order id
    XmlNodeCreater::BuildTextNode( kGoodsOrderIDNode, goods->_order_id, buffer );
    buffer.Append( kGoodsNodeEnd );
}
